/*本模块功能:课程管理_任课概要(管理员)
        编写者:朱光曜
        编写时间:2017.11.14*/
$(function() {
    var string1 = '<tr>' +
        '<th class="col-lg-3">序号</th>' +
        // '<th class="col-lg-5">开课院系</th>' +
        '<th class="col-lg-5">专业</th>' +
        '<th class="col-lg-6">班级/申请班</th>' +
        '<th class="col-lg-6">课程名称</th>' +
        '<th class="col-lg-5">任课教师</th>' +
        '<th class="col-lg-4">类型</th>' +
        '<th style="text-align: center" class="col-lg-5">操作</th>' +
        '</tr>';
    //列表头部
    var confirm1 = new Confirm;
    var Alert1 = new Alert;
    var Alert2 = new Alert;

    function locationInformationd(data) { //搜索

        var selectDepartment = ""; //院系
        for(key in data.departmentList) {
            selectDepartment += '<option value="1" class="optionList" departmentId="' + data.departmentList[key].departmentId + '">' + data.departmentList[key].departmentName + '</option>';
        }
        /*var major = ""; //专业
        for(key in data.professionList) {
            major += '<option value="1" professionId="' + data.professionList[key].professionId + '">' + data.professionList[key].professionName + '</option>';
        }*/

        return '<div class="fl inputRight"><label for="selectDepartment">院&ensp;&ensp;&ensp;&ensp;系:&ensp;</label>' +
            '<select id="selectDepartment" class="publicSelect" >' +
            '<option value="1">请选择</option>' +
            selectDepartment +
            '</select>' +
            '</div>' +
            '<div class="fl inputRight"><label for="selectMajor">专&ensp;&ensp;&ensp;&ensp;业:&ensp;</label>' +
            '<select id="selectMajor" class="publicSelect">' +
            '<option value="1">请选择</option>' +
            /*major +*/
            '</select>' +
            '</div>' +
            '<div class="fl inputRight"><label for="departNames">课程类型:&ensp;</label>'+
            '<select id="couSelectMark" class="publicSelect" >'+
            '<option value="">请选择</option>'+
            '<option value="1">教师排课</option>'+
            '<option value="2">学生选课</option>'+
            '</select>'+
            '</div>'+
            '<div class="searchBottom fl">'+
            '<div class="fl inputRight"><label for="curriculum">任课教师:&ensp;</label><input  type="text" id="curriculum" class="publicText" placeholder="请输入教师名称" /></div>' +
            '<div class="fr publicBtn searchBtn">搜索</div>'+
            '</div>';;
    }

    function tabelModel(data) { //列表

        var couSelectMarkCon = "";
        if(data.teaReCourseList[key].couSelectMark == 1) {
            couSelectMarkCon = "教师排课";
        } else if(data.teaReCourseList[key].couSelectMark == 2) {
            couSelectMarkCon = "学生选课 ";
        } else if(data.teaReCourseList[key].couSelectMark == 3) {
            couSelectMarkCon = "既排课又选课";
        }
        return '<tr data-num="0" classId="' + data.teaReCourseList[key].classId + '" courseId="' + data.teaReCourseList[key].courseId + '" stuorteaId="' + data.teaReCourseList[key].stuorteaId + '" couSelectMark="' + data.teaReCourseList[key].couSelectMark + '">' +
            '<td class="col-lg-2">' + ((parseInt(key) + 1)+(data.pageNow-1)*10) + '</td>' +
            // '<td class="col-lg-10"  title="' + data.teaReCourseList[key].departmentName + '">' + data.teaReCourseList[key].departmentName + '</td>' +
            '<td class="col-lg-10"  title="' + data.teaReCourseList[key].professionName + '">' + data.teaReCourseList[key].professionName + '</td>' +
            '<td class="col-lg-10"  title="' + data.teaReCourseList[key].className + '" >' + data.teaReCourseList[key].className + '</td>' +
            '<td class="col-lg-10"  title="' + data.teaReCourseList[key].courseName + '" >' + data.teaReCourseList[key].courseName + '</td>' +
            '<td class="col-lg-10"  title="' + data.teaReCourseList[key].userName + '" >' + data.teaReCourseList[key].userName + '</td>' +
            '<td class="col-lg-10"  title="' + couSelectMarkCon + '">' + couSelectMarkCon + '</td>' +

            '<td style="text-align: center" class="col-lg-4">' +
            '<a href="javascript:;" class="releaseLesson">解除任课</a>' +
            '</td>' +
            '</tr>';

    }
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    $(document).ajaxSend(function(){
        loading1.show();

    });
    $(document).ajaxStop(function(){
        loading1.hide();

    });
    document.addEventListener('DOMSubtreeModified',function(e){//去除空格
        $(".publicText").attr("maxlength","20");


        $.map($(".publicText"),function (opt) {//长度限制
            maxLengthLimit.init(opt);
        });
        $(".publicText").bind('input propertychange',function(){

            var regy = /\s/g;
            if(regy.test($(this).val())){
                $(this).val($(this).val().replace(/\s|\xA0/g,"")) ;
            }
        });
    });
    var nowData = 1;
    var index = {
        init: function() {
            this.bind();
        },
        bind: function() {
            var self = this;
            this.data_content = {}; //搜索集
            this.tab_content = {}; //列表集
            this.parameter = {
                "pageNow": 1,
                "pageSize": 10
            };

            this.postData(this.parameter, nowData);//加载列表

        },
        postData: function(par_data, nowData,hunt) { //获得搜索及列表数据
            var self = this;
            var parameter = par_data;

            $.ajax({
                type: "post",
                url: "skipTeaReCourseMana",
                async: true,
                data: {
                    "jsonStr": JSON.stringify(parameter)
                },
                success: function(data, status, xhr) {
                    var new_data = JSON.parse(data);
                    if(new_data.result == "success"){
                        if(hunt) {
                            self.tab_content = new_data;

                            self.tab_add();
                        } else {

                            self.data_content = new_data;
                            self.tab_content = new_data;

                            self.add();
                            self.tab_add();
                        }

                        var sumpage = new_data.totalPage;
                        if(sumpage<1){sumpage=1;};
                        var nowpage = 1;
                        var pagenumber = 10;
                        var parame = parameter;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {

                                nowpage = p;

                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                if(!parameter) {
                                    parameter = {
                                        "pageNow": nowpage,
                                        "pageSize": 10
                                    };

                                } else {
                                    parameter.pageNow = nowpage;

                                }
                                $.ajax({
                                    type: "post",
                                    url: "skipTeaReCourseMana",
                                    async: true,
                                    data: {
                                        "jsonStr": JSON.stringify(parameter)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        //获取地点
                                        var msgNew = JSON.parse(msg);
                                        self.tab_content = msgNew;

                                        self.tab_add();
                                    },
                                    error: function(data, status, xhr) {
                                        var json = JSON.stringify({
                                            "pageName": "error"
                                        });
                                        window.location.href = "skipPage?jsonStr=" + json;

                                    }
                                });
                            }
                        });
                    }else{
                        if(new_data.failMsg == "暂无权限"){
                            parent.location.reload(true);
                        }
                    }

                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        },
        add: function() { //搜索追加
            var self = this;
            var data = self.data_content;

            $("#searchCriteria").html(locationInformationd(data));

            $("#selectDepartment").on("change", function() {
                if($("#selectDepartment option:selected").attr("departmentid") == null) {

                    $("#selectMajor").html('<option value="1" >请选择</option>');
                } else {

                    var parameter = {
                        "departmentId": $("#selectDepartment option:selected").attr("departmentid")
                    };

                    $.ajax({
                        type: "post",
                        url: "getProfessionListByDepmentId",
                        async: true,
                        data: {
                            "jsonStr": JSON.stringify(parameter)
                        },
                        success: function(data) {
                            //获取地点

                            var msgNew = JSON.parse(data);
                            var con = "";

                            for(key in msgNew.professionList) {
                                con += '<option value="1" professionId="' + msgNew.professionList[key].professionId + '">' + msgNew.professionList[key].professionName + '</option>';
                            }
                            $("#selectMajor").html(con);

                        },
                        error: function(data, status, xhr) {
                            var json = JSON.stringify({
                                "pageName": "error"
                            });
                            window.location.href = "skipPage?jsonStr=" + json;

                        }
                    });
                }
            });
            $(".funBox .searchBtn").off("click").click(function() { //搜索点击

                var userName = $("#curriculum").val();
                var departmentId = $("#selectDepartment option:selected").attr("departmentId");
                var professionId = $("#selectMajor option:selected").attr("professionid");
                var couSelectMark=$("#couSelectMark").val();
                var nowData = 1;
                if(verification.specialCode(userName)!==true){
                    self.alert1("不能输入特殊字符(@/'\"#$%&^*)","fail");
                    return;
                }
                var parameter = {
                    "userName": userName,
                    "departmentId": departmentId,
                    "professionId": professionId,
                    "couSelectMark":couSelectMark,
                    "pageNow": 1,
                    "pageSize": 10
                };
                var j=0;
                //遍历删除空元素
                for(i in parameter){
                    if(j==0){j++;continue;}
                    if(parameter[i]==null||parameter[i]==""){
                        delete parameter[i];
                    }
                    if(!isNaN(parameter[i])){
                        parameter[i]=Number(parameter[i]);
                    }
                }


                self.postData(parameter, nowData,1);
            });

        },
        tab_add: function() { //列表追加

            var self = this;
            var data = self.tab_content;
            var table = string1;

            //data.selectList={};

            var i = 0;
            for(key in data.teaReCourseList) {
                i++;
            }

            if(i > 0) {

                for(key in data.teaReCourseList) {

                    table += tabelModel(data);
                }

                $(".mainContent .tableBox").html(table);
            } else {
                var trImg = "<tr><td colspan='8'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td/tr>";
                $(".mainContent .tableBox").html(string1 + trImg);
            }


            //设置列表宽度(最大为9)
            var numMax = 0;
            $(".tableBox tr th").each(function() {
                var str = $(this).attr("class");
                var num = str.lastIndexOf("col-lg-") + 7;
                var num1 = parseFloat(str.substring(num, num + 1));
                numMax += num1;
            });

            $(".tableBox tr th").each(function() {
                var str = $(this).attr("class");
                var num = str.lastIndexOf("col-lg-") + 7;
                var num1 = parseFloat(str.substring(num, num + 1));

                $(this).css("width",num1 / numMax * 100+ "%");
            });
            $(".releaseLesson").off("click").click(function() { //修改信息
                var  $_self = self;
                var  me=$(this);

                confirm1.contenthead="提示";
                confirm1.content="你确定要解除任课么";
                confirm1.success=function(){
                    var couSelectMark = me.parents("tr").attr("couSelectMark");
                    var classId = me.parents("tr").attr("classId");
                    var courseId = me.parents("tr").attr("courseId");
                    var stuorteaId = me.parents("tr").attr("stuorteaId");

                    var saveRelationPageParameters = {
                        "couSelectMark": Number(couSelectMark),
                        "classId": Number(classId),
                        "courseId": Number(courseId),
                        "stuorteaId": stuorteaId
                    };

                    $_self.submits(saveRelationPageParameters);
                };
                confirm1.init();
                confirm1.show();
            });

        },
        submits: function(saveRelationPageParameters) { //弹窗提交

            var self = this;
            $.ajax({
                type: "post",
                url: "unbundlingRelation",
                async: true,
                data: {
                    "jsonStr": JSON.stringify(saveRelationPageParameters)
                },

                success: function(msg) {

                    var msgNew = JSON.parse(msg);

                    if(msgNew.result == "success") {
                        self.alert1("操作成功");
                    } else {
                        //self.alert1("提交失败");
                        if(msgNew.failMsg){
                            self.alert1(msgNew.failMsg);
                        }else{
                            self.alert1("操作失败");
                        }

                    }

                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        },
        alert1: function(content1,state) { //confirm提示

            Alert1.contenthead = "提示";
            Alert1.content = content1;

            Alert1.success = function() {
                if(state=="fail"){

                }else{
                    window.location.reload();
                }

                Alert1.hide();
            };
            /*$(".alert_content" ).css({

                "line-height": "70px"
            })*/

            Alert1.init();
            Alert1.show();
        }

    };
    index.init();

});